﻿//===============================MainForm.cs==============================\\
//===Bart Steijlen======\\


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Team2_Vision
{
    public partial class MainForm : Form
    {
        List<VisionAlgorithm> AlgorithmList = new List<VisionAlgorithm>();
        Stopwatch stopWatch;
        String filter;
        List<Object> buffer = new List<object>();
        static int parameter = 0;
        PictureBox selectedPictureBox;
        int DrawMode = 0;
        static Color color = new Color();
        
        PictureBox outputPictureBox;

        Bitmap InputImage;
        public MainForm()
        {
            InitializeComponent();
            //AlgorithmList.Add(new FlipAlgorithm("Flip"));
            //AlgorithmList.Add(new RotateAlgorithm("Rotate"));

            
           
           // AlgorithmList.Add(new Median_fast("faster QuickSort"));
                 
              
            
            /*          ^
            *          /|\
            *           |
            */
            
        }

        private void openPictureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openImage(pictureBox1);
        }
        private void openImage(PictureBox picturebox)
        {
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            oFD.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.PNG;)|*.BMP;*.JPG;*.JPEG;*.PNG;";
            oFD.FilterIndex = 1;
            oFD.RestoreDirectory = true;
            if (oFD.ShowDialog() == DialogResult.OK)
            {
                String CurrentFileName = Path.GetFullPath(oFD.FileName);
                InputImage = new Bitmap(CurrentFileName);
                picturebox.Image = InputImage;
            }
        }

        private void AlgoListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }





        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

       

        private void ClearButton_Click(object sender, EventArgs e)
        {
            InfoLabel.Text = "";
        }

        private void backgroundAlgorithmWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Error: " + e.Error.Message);
            }
            else if (e.Cancelled)
            {
                MessageBox.Show("Algorithm canceled.");
            }
            else
            {
                stopWatch.Stop();
                long elapsedms = stopWatch.ElapsedMilliseconds;
                InfoLabel.Text += filter + " took: " + elapsedms + " ms\n";
                parameter = 0;
                EnableAllInputs(true);
            }
        }

        private void backgroundAlgorithmWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            System.ComponentModel.BackgroundWorker worker;
            worker = (System.ComponentModel.BackgroundWorker)sender;
            List<object> argList = (List<object>)e.Argument;
            VisionAlgorithm visAlg = (VisionAlgorithm) argList.ElementAt(0);
            Bitmap InputImage = (Bitmap)argList.ElementAt(1);
            
            PictureBox OutputPictureBox = (PictureBox)argList.ElementAt(2);
            OutputPictureBox.Image = visAlg.DoAlgorithm(InputImage, parameter);

        }

        private void EnableAllInputs(bool state)
        {
           
            pictureBox1.Enabled = state;
            pictureBox2.Enabled = state;
            pictureBox3.Enabled = state;
        }

        private void startAlgThread(VisionAlgorithm filter)
        {
            List<object> argList = new List<object>();
            argList.Add(filter);
            if (selectedPictureBox != null)
            {
                if (selectedPictureBox.Image != null)
                {
                    argList.Add(selectedPictureBox.Image);

                    if (!radioButton4.Checked)
                    {
                        argList.Add(outputPictureBox);
                    }
                    else
                    {
                        argList.Add(selectedPictureBox);
                    }
                    stopWatch = new Stopwatch();
                    EnableAllInputs(false);
                    stopWatch.Start();
                    backgroundAlgorithmWorker.RunWorkerAsync(argList);
                    Console.WriteLine("Ja ik kom hier");
                }
            }
        }

        public static void setParameter(int waarde)
        {
            parameter = waarde;
        }



        private void medianToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm median = new Median("median");
            filter = "Median";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give Matrix Size:";
            para.ParameterSliderMatrix.Visible = true;
            para.ParameterSliderPercentage.Visible = false;
            para.ParameterTextBox.Text = 3 + "";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(median);
            }            
        }

        private void impulseNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new ImpulseNoise("impulse noise");
            filter = "Impulse noise";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give Noise Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(Noise);
            }            
        }

        private void bitFlipNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new BitFlipNoise("Bit Flip noise");
            filter = "Bit Flip noise";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give Noise Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(Noise);
            }            
        }

        private void randomNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new RandomNoise("Random noise");
            filter = "Random noise";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give Noise Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(Noise);
            }            
        }

        private void sharpenToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Sharpen = new Sharpen("Sharpen");
            filter = "Sharpen";
            startAlgThread(Sharpen);
        }

        private void boxBlurToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Blur = new BoxBlur("Box Blur");
            filter = "Box Blur";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give Matrix Size:";
            para.ParameterSliderMatrix.Visible = true;
            para.ParameterSliderPercentage.Visible = false;
            para.ParameterTextBox.Text = 3 + "";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(Blur);
            }  
        }

        private void boxBlurColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Blur = new BoxBlurColor("Box Blur Color");
            filter = "Box Blur Color";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give Matrix Size:";
            para.ParameterSliderMatrix.Visible = true;
            para.ParameterSliderPercentage.Visible = false;
            para.ParameterTextBox.Text = 3 + "";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(Blur);
            } 
        }

        private void grayScaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Color = new GrayScale("Gray Scale");
            filter = "Gray Scale";
            startAlgThread(Color);
        }

        private void OutputGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void InputPictureBox_DoubleClick(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                Form ipForm = new ImagePreviewForm(new Bitmap(pictureBox1.Image));
                ipForm.Show();
            }
        }

  

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (DrawMode == 0)
                {
                    PictureBoxFrame1.BackColor = Color.MediumSlateBlue;
                    PictureBoxFrame2.BackColor = Color.LightSteelBlue;
                    PictureBoxFrame3.BackColor = Color.LightSteelBlue;
                    selectedPictureBox = pictureBox1;
                    if (pictureBox1.Image != null)
                    {
                        pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All);
                    }

                }
                if (DrawMode == 1)
                {
                    Point punt = new Point(e.X, e.Y);
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                if (pictureBox1.Image != null)
                {
                    Point punt = new Point(e.X, e.Y);
                    ContextMenuPB1.Location = punt;
                    ContextMenuPB1.Show(pictureBox1, punt);
                }
                else
                {
                    openImage(pictureBox1);
                }
            }


        }

        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBoxFrame2.BackColor = Color.MediumSlateBlue;
                PictureBoxFrame1.BackColor = Color.LightSteelBlue;
                PictureBoxFrame3.BackColor = Color.LightSteelBlue;
                selectedPictureBox = pictureBox2;
                if (pictureBox2.Image != null)
                {
                    pictureBox2.DoDragDrop(pictureBox2.Image, DragDropEffects.All);
                }

            }

            if (e.Button == MouseButtons.Right)
            {
                if (pictureBox2.Image != null)
                {
                    Point punt = new Point(e.X, e.Y);
                    contextMenuPB2.Location = punt;
                    contextMenuPB2.Show(pictureBox2, punt);
                }
                else
                {
                    openImage(pictureBox2);
                }

            }
        }

        private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBoxFrame3.BackColor = Color.MediumSlateBlue;
                PictureBoxFrame2.BackColor = Color.LightSteelBlue;
                PictureBoxFrame1.BackColor = Color.LightSteelBlue;
                selectedPictureBox = pictureBox3;
                
                if (pictureBox3.Image != null)
                {
                    pictureBox3.DoDragDrop(pictureBox3.Image, DragDropEffects.All);
                    
                } 
            }
            if (e.Button == MouseButtons.Right)
            {
                if (pictureBox3.Image != null)
                {
                    Point punt = new Point(e.X, e.Y);
                    contextMenuPB3.Location = punt;
                    contextMenuPB3.Show(pictureBox3, punt);
                }
                else
                {
                    openImage(pictureBox3);
                }

            }
        }
        private void pictureBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
                e.Effect = DragDropEffects.None;
        }

        private void pictureBox2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Move;                
            }
            else
                e.Effect = DragDropEffects.None;            
        }
        private void pictureBox3_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
                e.Effect = DragDropEffects.None;
        }

        private void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            if ((e.Data.GetDataPresent(DataFormats.Bitmap)))
            {

                pictureBox1.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
            }
        }

        private void pictureBox2_DragDrop(object sender, DragEventArgs e)
        {
            if ((e.Data.GetDataPresent(DataFormats.Bitmap)))
            {

                pictureBox2.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));              

            }  
        }
        private void pictureBox3_DragDrop(object sender, DragEventArgs e)
        {
            if ((e.Data.GetDataPresent(DataFormats.Bitmap)))
            {

                pictureBox3.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));

            }
        }

        private void ClearPictureBox1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
        }

        private void SaveFilePB1(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                Bitmap bmp = (Bitmap)pictureBox1.Image;
                SaveFileDialog sv = new SaveFileDialog();
                sv.ShowDialog();
                String filename = sv.FileName;
                String directory = sv.InitialDirectory;
                String extension = ".Jpeg";
                bmp.Save(directory + filename + extension, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

        private void ClearPB1(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
        }

        private void MakeHistoPB1(object sender, EventArgs e)
        {
            int[] yas = new int[100000];
            HistoAlgorithm Histogram = (HistoAlgorithm)new MakeHistogram("");
            Bitmap img = (Bitmap)pictureBox1.Image;
            yas = Histogram.DoAlgorithm(img);
            Form Chart = new Chart(yas);
            Chart.Show();
        }

        private void SaveFilePB2(object sender, EventArgs e)
        {
            if (pictureBox2.Image != null)
            {
                Bitmap bmp = (Bitmap)pictureBox2.Image;
                SaveFileDialog sv = new SaveFileDialog();
                sv.ShowDialog();
                String filename = sv.FileName;
                String directory = sv.InitialDirectory;
                String extension = ".Jpeg";
                bmp.Save(directory + filename + extension, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

        private void ClearPB2(object sender, EventArgs e)
        {
            pictureBox2.Image = null;
        }

        private void MakeHistoPB2(object sender, EventArgs e)
        {
            int[] yas = new int[100000];
            HistoAlgorithm Histogram = (HistoAlgorithm)new MakeHistogram("");
            Bitmap img = (Bitmap)pictureBox2.Image;
            yas = Histogram.DoAlgorithm(img);
            Form Chart = new Chart(yas);
            Chart.Show();
        }

        private void SaveFilePB3(object sender, EventArgs e)
        {
            if (pictureBox3.Image != null)
            {
                Bitmap bmp = (Bitmap)pictureBox3.Image;
                SaveFileDialog sv = new SaveFileDialog();
                sv.ShowDialog();
                String filename = sv.FileName;
                String directory = sv.InitialDirectory;
                String extension = ".Jpeg";
                bmp.Save(directory + filename + extension, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

        private void ClearPB3(object sender, EventArgs e)
        {
            pictureBox3.Image = null;
        }

        private void MakeHistoPB3(object sender, EventArgs e)
        {
            int[] yas = new int[100000];
            HistoAlgorithm Histogram = (HistoAlgorithm)new MakeHistogram("");
            Bitmap img = (Bitmap)pictureBox3.Image;
            yas = Histogram.DoAlgorithm(img);
            Form Chart = new Chart(yas);
            Chart.Show();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void oCRToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            OCRForm ocr = new OCRForm();
            ocr.Show();
            this.Visible = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            outputPictureBox = pictureBox1;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            outputPictureBox = pictureBox2;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            outputPictureBox = pictureBox3;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            outputPictureBox = selectedPictureBox;
        }

        private void pictureBox1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openImage(pictureBox1);
        }

        private void pictureBox2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openImage(pictureBox2);
        }

        private void pictureBox3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openImage(pictureBox3);
        }

        private void label2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://onderwijsteams.sharepoint.hu.nl/fnt/Cluster_ICT/thema_ti_vig/pages/home.aspx");
        }

        private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            pictureBox2.Image = null;
            pictureBox3.Image = null;
            InfoLabel.Text = null;
        }

        private void selectColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog newDialog = new ColorDialog();
            newDialog.ShowDialog();
            color = newDialog.Color;
            
            
        }

        private void onToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DrawMode = 1;
        }

        private void offToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DrawMode = 0;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

        private void ruinClusterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new RuinCluster("Random noise");
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give Ruin Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(Noise);
            }
        }

        private void ruinLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm ruin = new RuinLines("Ruin");
            filter = "Ruin Cluster";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give intensity:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(ruin);
            } 
        }

        private void ruinPixelsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm ruin = new RuinPixels("Ruin");
            filter = "Ruin Cluster";
            ParameterForm para = new ParameterForm(1);
            para.ParameterText.Text = "Give intensity:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "";
            para.ShowDialog();
            if (parameter != 0)
            {
                startAlgThread(ruin);
            } 
        }

  

   

     



        



    }
}
